package servlet;

import pub.fjf.http.HttpServlet;
import pub.fjf.http.HttpServletRequest;
import pub.fjf.http.HttpServletResponse;
import pub.fjf.http.ProxyServer;
import pub.fjf.http.ResourceServer;
import pub.fjf.http.SessionServer;
import pub.fjf.http.WebProject;
import pub.fjf.tools.FileTools;
import pub.fjf.tools.Json;
import pub.fjf.tools.StringTools;

public class WebProjectConfig extends HttpServlet{

	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp) {
		// TODO Auto-generated method stub
		int index=0;
		try {
			index=Integer.parseInt(req.getParameter());
		}catch(NumberFormatException e) {
			
		}
		String serverConfigPath=req.getClient().getServer().getConfigPath();
		Json serverConfig=new Json(StringTools.readAsPath(serverConfigPath));
		Json project=serverConfig.getMember("webProjects").getElement(index);
		WebProject webProject=req.getClient().getServer().getWebProjects().get(index);
		String projectConfigPath=webProject.getRootPath();
		if(projectConfigPath.length()>0) {
			projectConfigPath+="/";
		}
		projectConfigPath+=project.getMember("configPath").getValueString();
		Json projectConfig=new Json(StringTools.readAsPath(projectConfigPath));
		String filtersConfigPath=webProject.getRootPath();
		if(filtersConfigPath.length()>0) {
			filtersConfigPath+="/";
		}
		filtersConfigPath+=projectConfig.getMember("filtersConfig").getValueString();
		Json filtersConfig=new Json(StringTools.readAsPath(filtersConfigPath));
		Json respJson=new Json(Json.TYPE_OBJECT);
		respJson.setMember("index", new Json(""+index));
		respJson.setMember("project", project);
		respJson.setMember("projectConfig", projectConfig);
		respJson.setMember("filtersConfig", filtersConfig);
		resp.sendData(respJson.toString(), "text/txt");
	}

	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp) {
		// TODO Auto-generated method stub
		Json configJson=new Json(req.getBody());
		int index=Integer.parseInt(configJson.getMember("index").getValueString());
		Json projectJson=configJson.getMember("project");
		Json projectConfigJson=configJson.getMember("projectConfig");
		Json filtersConfigJson=configJson.getMember("filtersConfig");
		
		String serverConfigPath=req.getClient().getServer().getConfigPath();
		Json serverConfig=new Json(StringTools.readAsPath(serverConfigPath));
		Json project=serverConfig.getMember("webProjects").getElement(index);
		WebProject webProject=req.getClient().getServer().getWebProjects().get(index);
		project.setMember("name", projectJson.getMember("name"));
		webProject.setName(project.getMember("name").getValueString());
		if(FileTools.move(project.getMember("rootPath").getValueString(), projectJson.getMember("rootPath").getValueString())) {
			project.setMember("rootPath", projectJson.getMember("rootPath"));
			webProject.setRootPath(project.getMember("rootPath").getValueString());
		}
		StringTools.writeAsPath(serverConfigPath, serverConfig.toString(true));
		
		String projectConfigPath=webProject.getRootPath();
		if(projectConfigPath.length()>0) {
			projectConfigPath+="/";
		}
		projectConfigPath+=project.getMember("configPath").getValueString();
		Json projectConfig=new Json(StringTools.readAsPath(projectConfigPath));
		projectConfig.setMember("url", projectConfigJson.getMember("url"));
		webProject.setUrl(projectConfig.getMember("url").getValueString());
		StringTools.writeAsPath(projectConfigPath, projectConfig.toString(true));
		
		String filtersConfigPath=webProject.getRootPath();
		if(filtersConfigPath.length()>0) {
			filtersConfigPath+="/";
		}
		filtersConfigPath+=projectConfig.getMember("filtersConfig").getValueString();
		Json filtersConfig=new Json(StringTools.readAsPath(filtersConfigPath));
		int filterIndex=-1;
		Json filterConfig = null;
		for(int i=0;i<filtersConfig.getElementCount();i++) {
			filterConfig=filtersConfig.getElement(i);
			if(filterConfig.getMember("filterName").getValueString().equals("ResourceServer")) {
				filterIndex=i;
				break;
			}
		}
		if(filterIndex>=0) {
			Json resourceConfig=filtersConfigJson.getMember("resourceConfig");
			ResourceServer resourceServer=webProject.getResourceServer();
			filterConfig.getMember("config").setMember("rootPath", resourceConfig.getMember("rootPath"));
			resourceServer.setRootPath(filterConfig.getMember("config").getMember("rootPath").getValueString());
			filterConfig.getMember("config").setMember("indexPath", resourceConfig.getMember("indexPath"));
			resourceServer.setIndexPath(filterConfig.getMember("config").getMember("indexPath").getValueString());
			filterConfig.getMember("config").setMember("fileReadLimit", resourceConfig.getMember("fileReadLimit"));
			resourceServer.setFileReadLimit(filterConfig.getMember("config").getMember("fileReadLimit").toInt());
		}
		filterIndex=-1;
		for(int i=0;i<filtersConfig.getElementCount();i++) {
			filterConfig=filtersConfig.getElement(i);
			if(filterConfig.getMember("filterName").getValueString().equals("SessionServer")) {
				filterIndex=i;
				break;
			}
		}
		if(filterIndex>=0) {
			Json sessionConfig=filtersConfigJson.getMember("sessionConfig");
			SessionServer sessionServer=webProject.getSessionServer();
			filterConfig.getMember("config").setMember("SESSION_ID", sessionConfig.getMember("SESSION_ID"));
			sessionServer.setSESSION_ID(filterConfig.getMember("config").getMember("SESSION_ID").getValueString());
			filterConfig.getMember("config").setMember("maxLiveTime", sessionConfig.getMember("maxLiveTime"));
			sessionServer.setMaxLiveTime(filterConfig.getMember("config").getMember("maxLiveTime").toInt());
			filterConfig.getMember("config").setMember("checkTime", sessionConfig.getMember("checkTime"));
			sessionServer.setCheckTime(filterConfig.getMember("config").getMember("checkTime").toInt());
		}
		filterIndex=-1;
		for(int i=0;i<filtersConfig.getElementCount();i++) {
			filterConfig=filtersConfig.getElement(i);
			if(filterConfig.getMember("filterName").getValueString().equals("ProxyServer")) {
				filterIndex=i;
				break;
			}
		}
		if(filterIndex>=0) {
			Json proxyConfig=filtersConfigJson.getMember("proxyConfig");
			ProxyServer proxyServer=webProject.getProxyServer();
			filterConfig.getMember("config").setMember("charset", proxyConfig.getMember("proxyCharset"));
			proxyServer.setCharset(filterConfig.getMember("config").getMember("charset").getValueString());
			filterConfig.getMember("config").setMember("receiveTimeOut", proxyConfig.getMember("proxyReceiveTimeOut"));
			proxyServer.setReceiveTimeOut(filterConfig.getMember("config").getMember("receiveTimeOut").toInt());
			filterConfig.getMember("config").setMember("continueTimeOut", proxyConfig.getMember("continueTimeOut"));
			proxyServer.setContinueTimeOut(filterConfig.getMember("config").getMember("continueTimeOut").toInt());
		}
		StringTools.writeAsPath(filtersConfigPath, filtersConfig.toString(true));
		
		Json respJson=new Json(Json.TYPE_OBJECT);
		respJson.setMember("index", new Json(""+index));
		respJson.setMember("project", project);
		respJson.setMember("projectConfig", projectConfig);
		respJson.setMember("filtersConfig", filtersConfig);
		resp.sendData(respJson.toString(), "text/txt");
	}

}
